﻿using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;
using System.Diagnostics;

namespace XamlingCore.AzureHelper
{

    public class AzureDriveHelper
    {
        CloudStorageAccount cloudStorageAccount;
        
        private CloudDrive drive;

        private string driveLetter;
        private string uri;
        public AzureDriveHelper(string containerName, string fileName)
        {
            AzureBlobHelper blob = new AzureBlobHelper(containerName);

            LocalResource localCache = RoleEnvironment.GetLocalResource("DriveCache");

            CloudDrive.InitializeCache(localCache.RootPath + "cache",
              localCache.MaximumSizeInMegabytes);

            drive = blob.GetDrive(fileName);

            uri = blob.GetUri(fileName);

        //    CloudDrive.InitializeCache(localCache.RootPath,
        //localCache.MaximumSizeInMegabytes);
        //    CloudDrive drive = new CloudDrive(BlobURI, credentials);
        //    drive.Create(sizeOfDrive);
        //    string driveLetter = string.Empty;
        //    driveLetter = drive.Mount(cacheSize, DriveMountOptions.None);)



            try
            {
                drive.Create(10240);
            }catch(CloudDriveException e)
            {
                Debug.WriteLine(e.ToString());
                //throw e;
            }
            
        }

        public string Uri
        {
            get
            {
                return uri;
            }
        }

        public void Mount()
        {
            driveLetter = drive.Mount(1024, DriveMountOptions.None);
        }

        public void UnMount()
        {
            drive.Unmount();
        }

        public string DriveLetter
        {
            get
            {
                return driveLetter;
            }
        }
    }
}
